"""
启动类
@author tangn
"""
import time

import uvicorn
from fastapi import FastAPI, Request

from exception.ExceptionHandler import ExceptionHandler
from router.RouterHandler import RouterHandler

# 需要引入相对应的controller

app = FastAPI(
    # API 文件名称
    title='FastBoot API 文档',
    # API 文件描述
    description='## **Make fastapi as easy to use as spring boot** '
                '\n\n 这里面会涉及到两张表，order_info和user_info，这是做最基础的数据演示'
                '\n\n [项目代码:https://gitee.com/bean1995/fastboot](https://gitee.com/bean1995/fastboot)'
                '\n\n [官方文档:https://fastapi.tiangolo.com/](https://fastapi.tiangolo.com/)',
    # API 版本号
    version='1.0.0',
    # 对应swagger文档地址
    docs_url='/docs',
    # 对应阅读文档地址
    redoc_url='/redocs',
)
# 配置路由
RouterHandler.register(app)
# 注册捕获异常
ExceptionHandler.register(app)


@app.on_event("startup")
async def startup_event():
    print("项目已启动..")


@app.middleware("http")
async def add_process_time_header(request: Request, call_next):
    start_time = time.time()
    response = await call_next(request)
    print(type(response))
    print(response.__dict__)
    process_time = time.time() - start_time
    return response


if __name__ == '__main__':
    # 这里启动时，必须要对应好 application --> 文件名  app --> 对应定义好的名字
    uvicorn.run('application:app', host='127.0.0.1', port=8888, reload=True, debug=True, workers=1)
